//
//  NSObject+ACORMExtension.m
//  ACCache_Example
//
//  Created by autohome on 2021/8/6.
//  Copyright © 2021 CaiXiang. All rights reserved.
//

#import "NSObject+ACORMExtension.h"
#import <objc/runtime.h>

NSString *const kAutoprimarykey = @"ac_Autoprimarykey";

NSString *const SqlTypeReal = @"real";
NSString *const SqlTypeInteger = @"integer";
NSString *const SqlTypeBool = @"bool";
NSString *const SqlTypeText = @"text";
NSString *const SqlTypeBlob = @"blob";

static char kAC_autoPrimaryKey;
static char kAC_LastInsertTime;

@implementation NSObject (ACORMExtension)

- (long)autoPrimaryKey {
    NSNumber* number = objc_getAssociatedObject(self, &kAC_autoPrimaryKey);
    return [number longValue];
}

- (void)setAutoPrimaryKey:(long)autoPrimaryKey {
    objc_setAssociatedObject(self,&kAC_autoPrimaryKey,[NSNumber numberWithUnsignedLong:autoPrimaryKey],OBJC_ASSOCIATION_RETAIN);
}

- (NSTimeInterval)lastInsertTime{
    NSNumber* number = objc_getAssociatedObject(self, &kAC_LastInsertTime);
    return [number longLongValue];
}

- (void)setLastInsertTime:(NSTimeInterval)lastInsertTime{
    objc_setAssociatedObject(self,&kAC_LastInsertTime,[NSNumber numberWithUnsignedLong:lastInsertTime],OBJC_ASSOCIATION_RETAIN);
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if ([key isEqualToString:kAutoprimarykey]) {
        self.autoPrimaryKey = [value longValue];
    }
}

- (id)valueForUndefinedKey:(NSString *)key {
    if ([key isEqualToString:kAutoprimarykey]) {
        return @(self.autoPrimaryKey);
    }
    return nil;
}

+ (void)debugHandler:(void(^)(void))handler {
#if DEBUG
    dispatch_async(dispatch_get_main_queue(), ^{
        !handler?:handler();
    });
#endif
}

@end
